﻿namespace PomodoroTool.Core
{
    #region

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Linq;
    using System.Xml.Serialization;

    #endregion

    public class PomodoroController
    {
        #region Constants and Fields

        private const string CompleteRegex = @"\[d\]";

        private const string CompleteTag = @"[d]";

        private const string PomodoroRegex = @"\[p\]";

        private const string PomodoroTag = @"[p]";

        private const string TaskTag = "[t]";

        private List<Task> _tasks = new List<Task>();

        #endregion

        #region Constructors and Destructors

        public PomodoroController()
        {
        }

        #endregion

        #region Properties

        public List<Task> Tasks
        {
            get
            {
                return _tasks;
            }
        }

        public bool HasActivePomodoro
        {
            get
            {
                return _tasks.Any(q => q.Active);
            }
        }

        #endregion

        #region Public Methods

       

        #endregion

        #region Methods

       
        #endregion

        public class Task
        {
            #region Constants and Fields

            private List<string> _taskLines = new List<string>();

            #endregion

            #region Properties

            public bool Completed { get; set; }

            public int LineFound { get; set; }

            public string[] Lines
            {
                get
                {
                    return _taskLines.ToArray();
                }
                set
                {
                    _taskLines = value.ToList();
                }
            }

            public int Pomodoros { get; set; }

            public bool Active { get; set; }

            #endregion

            #region Public Methods

            public void AddLine(string line)
            {
                _taskLines.Add(line);
            }

            #endregion
        }

        public void PomodoroDone()
        {
            var activeTask = _tasks.FirstOrDefault(q => q.Active);
            if(activeTask != null)
            {
                activeTask.Pomodoros++;
            }
        }

        public void StoreTasksTo(IsolatedStorageFile store)
        {
            using(var file = new IsolatedStorageFileStream("tasks.xml", FileMode.Create, FileAccess.Write, store))
            {
                var ser = new XmlSerializer(_tasks.GetType());
                ser.Serialize(file, _tasks);
            }
        }
        public void LoadTasks(IsolatedStorageFile store)
        {
            try
            {
                if (store.GetFileNames("tasks.xml").Any())
                {
                    using (var file = new IsolatedStorageFileStream("tasks.xml", FileMode.Open, FileAccess.Read, store))
                    {
                        var ser = new XmlSerializer(_tasks.GetType());
                        _tasks = (List<Task>)ser.Deserialize(file);
                    }
                }
            } catch(Exception e)
            {
            }

        }
    }
}